package com.sky.task;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.dto.CategoryPageQueryDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.entity.Category;
import com.sky.mapper.CategoryMapper;
import com.sky.mapper.DishMapper;
import com.sky.result.PageResult;
import com.sky.vo.DishVO;
import lombok.extern.slf4j.Slf4j;
import org.redisson.api.RLock;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.concurrent.TimeUnit;

@Component
@Slf4j
public class PageTask {
    @Autowired
    RedissonClient redissonClient;
    @Autowired
    DishMapper dishMapper;
    @Autowired
    CategoryMapper categoryMapper;
    @Autowired
    private RedisTemplate redisTemplate;

    @Scheduled(cron="0 0 0 * * *")
    public void doCachePageResource(){
        RLock rLock=redissonClient.getLock("heima:precachejob:docache:lock");
        try {
            //只有一个线程能获取到锁
            boolean isLock = rLock.tryLock(0, -1, TimeUnit.SECONDS);
            if(isLock){
                //获得当前线程的id
                log.info("开锁:{}",Thread.currentThread().getId());
                //使用pagehelper来帮忙实现分页管理
                PageHelper.startPage(1,10);
                //获取dish的page对象
                DishPageQueryDTO dishPageQueryDTO=new DishPageQueryDTO();
                Page<DishVO> dishPage =dishMapper.pageQuery(dishPageQueryDTO);
                //封装page对象
                long totalDish = dishPage.getTotal();
                List<DishVO> resultDish = dishPage.getResult();
                PageResult dishPageResult = new PageResult(totalDish, resultDish);

                //获取category的page对象
                CategoryPageQueryDTO categoryPageQueryDTO=new CategoryPageQueryDTO();
                Page<Category> categoryPage=categoryMapper.pageQuery(categoryPageQueryDTO);
                long totalCategory=categoryPage.getTotal();
                List<Category> resultCategory = categoryPage.getResult();
                PageResult categoryPageResult = new PageResult(totalCategory, resultCategory);

                //获得key
                String redisKeyDish=String.format("heima:page:resource:dish");
                String redisKeyCategory=String.format("heima:page:resource:category");
                ValueOperations<String,Object> valueOperations = redisTemplate.opsForValue();

                //写缓存
                try{
                    valueOperations.set(redisKeyDish,dishPageResult,30000,TimeUnit.MILLISECONDS);
                    valueOperations.set(redisKeyCategory,categoryPageResult,30000,TimeUnit.MILLISECONDS);
                } catch (Exception e) {
                    log.error("redis set key error",e);
                }
            }
        }catch (InterruptedException e){
            log.error("doCachePageResource error",e);
        }finally {
            //只能释放自己的锁
            if(rLock.isHeldByCurrentThread()){
                log.info("解锁:{}",Thread.currentThread().getId());
                rLock.unlock();
            }
        }
    }
}
